/*
 * @(#) BasGeneralAction.java  1.0  2004-4-15
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas.web;

import com.cnc.bas.IBasService;
import com.cnc.bas.seq.ISeqService;
import com.cnc.bas.util.Constants;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * User: Du bf
 * Date: 2004-4-15
 * Time: 9:21:42
 */
public class BasGeneralAction extends SecureBaseDispatchAction {
    public IBasService basService = null;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        basService = (IBasService) this.getBean(Constants.BEAN_NAME_BAS);
    }

    public ActionForward list(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        BasGeneralForm basForm = ((BasGeneralForm) form);
        List list = basService.list(basForm.getLtData(), basForm.getOrder());
        request.setAttribute("list", list);
        return mapping.findForward("list");
    }

    public ActionForward add(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        BasGeneralForm basForm = ((BasGeneralForm) form);
        basForm.processCheckboxField();
        basForm.setInputInfo();
        basForm.setUpdateInfo();
        if ((basForm.isIdNumber()) && (basForm.getIdField() != null)) {
            ISeqService seqService = (ISeqService) this.getBean("seqService");
            String classname = basForm.getLtData().getClass().getName();
            String[] name = classname.split("\\x2E");
            Long id = seqService.getID(name[name.length - 1] + "Id");
            basForm.setIdValue(id);
        }
        basService.add(basForm.getLtData());
        return mapping.findForward("edit");
    }

    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        BasGeneralForm basForm = ((BasGeneralForm) form);
        basForm.processCheckboxField();
        basForm.setUpdateInfo();
        basService.update(basForm.getLtData());
        return mapping.findForward("edit");
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        BasGeneralForm basForm = ((BasGeneralForm) form);
        if (basForm.isIdNumber())
            basService.delete(((BasGeneralForm) form).getLtData().getClass(), new Long(Long.parseLong(id)));
        else
            basService.delete(((BasGeneralForm) form).getLtData().getClass(), id);
        return mapping.findForward("list");
    }

    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        Object obj = null;
        BasGeneralForm basForm = ((BasGeneralForm) form);
        if (basForm.isIdNumber())
            obj = basService.load(((BasGeneralForm) form).getLtData().getClass(), new Long(Long.parseLong(id)));
        else
            obj = basService.load(((BasGeneralForm) form).getLtData().getClass(), id);
        ((BasGeneralForm) form).setLtData(obj);
        return mapping.findForward("edit");
    }

    public ActionForward create(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        return mapping.findForward("edit");
    }
}

